<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>PDF 查看器</title>
  <style>
    body {
      margin: 0;
      padding: 20px;
      font-family: Arial, sans-serif;
      background: #f5f5f5;
    }
    #pdf-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 15px;
    }
    canvas {
      border: 1px solid #ddd;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
  </style>
</head>
<body>
  <!-- ✅ 必须有这个容器 -->
  <div id="pdf-container"></div>

  <script type="module">
    import * as pdfjsLib from './lib/pdf.min.mjs';
    window.pdfjsLib = pdfjsLib;

    // ✅ 指向正确的 worker 文件
    pdfjsLib.GlobalWorkerOptions.workerSrc = './lib/pdf.worker.min.mjs';

    async function init() {
      try {
        // ✅ 修改路径：test.pdf 在根目录，不是 lib/ 下
        const loadingTask = pdfjsLib.getDocument('./lib/test.pdf');
        const pdf = await loadingTask.promise;
        console.log('📄 PDF 加载成功，共', pdf.numPages, '页');

        // ✅ 获取容器
        const container = document.getElementById('pdf-container');

        // 渲染每一页
        for (let i = 1; i <= pdf.numPages; i++) {
          const page = await pdf.getPage(i);
          const viewport = page.getViewport({ scale: 1.5 });
          const canvas = document.createElement('canvas');
          canvas.width = viewport.width;
          canvas.height = viewport.height;
          const ctx = canvas.getContext('2d');
          await page.render({ canvasContext: ctx, viewport }).promise;
          container.appendChild(canvas);
        }
      } catch (error) {
        console.error('❌ 加载失败:', error);
      }
    }

    init();
  </script>
</body>
</html>